<template>
	<div class="system-dic-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-input v-model="state.tableData.param.query" size="default" placeholder="请输入名称" style="max-width: 180px"></el-input>
				<el-button size="default" type="primary" class="ml10" @click="getTableData">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddResource('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增机器
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" width="100" />
				<el-table-column prop="name" label="IP" show-overflow-tooltip></el-table-column>
				<el-table-column prop="description" label="描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="200">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditResource('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="onOpenPrivilege(scope.row)">授权</el-button>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.page"
				background
				v-model:page-size="state.tableData.param.size"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<ResourceDialog ref="resourceDialogRef" @refresh="getTableData()" />
		<PrivilegeDialog ref="privilegeDialogRef" />
	</div>
</template>

<script setup lang="ts" name="systemDic">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { resourceApi } from '/@/api/resource';
import Constants from '/@/constant/constants';

// 引入组件
const ResourceDialog = defineAsyncComponent(() => import('/@/views/resource/server/dialog.vue'));
const PrivilegeDialog = defineAsyncComponent(() => import('/@/views/resource/privilege/privilegeDialog.vue'));

// 定义变量内容
const resourceDialogRef = ref();
const privilegeDialogRef = ref();

const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			query: '',
			type: Constants.RESOURCE_TYPE.SERVER,
			page: 1,
			size: 10,
		},
	},
});

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	resourceApi()
		.pageResource(state.tableData.param)
		.then((res) => {
			state.tableData.data = res.data.records;
			state.tableData.total = res.data.total;
			setTimeout(() => {
				state.tableData.loading = false;
			}, 500);
		})
		.catch((err) => {
			state.tableData.loading = false;
			ElMessage.error(err.msg);
		});
};
// 打开新增资源弹窗
const onOpenAddResource = (type: string) => {
	resourceDialogRef.value.openDialog(type);
};
// 打开修改资源弹窗
const onOpenEditResource = (type: string, row: Object) => {
	resourceDialogRef.value.openDialog(type, row);
};

const onOpenPrivilege = (row: Object) => {
	privilegeDialogRef.value.openDialog(state.tableData.param.type, row);
};

// 删除字典
const onRowDel = (row: RowDicType) => {
	ElMessageBox.confirm(`此操作将永久删除字典名称：“${row.name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			resourceApi()
				.deleteResource({ id: row.id })
				.then(() => {
					getTableData();
					ElMessage.success('删除成功');
				});
		})
		.catch(() => {});
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
